package javalab2022;

import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;

abstract class Lottery {
	protected int winNumber[];
	protected int userNumber[];
	protected String userInput;

//	Lottery() {
//		winNumber = createWinNumber();
//	}
	Lottery(String s){
		winNumber=createWinNumber(s);
	}

	int[] createWinNumber() {
		int[] winnu = new int[3];
		Random rd = new Random();
		for (int i = 0; i < 3; i++) {
			winnu[i] = rd.nextInt(10);
		}
		return winnu;
	}
	int[] createWinNumber(String s) {
		int[] winnu = new int[3];
		try {
			Integer i = Integer.valueOf(s);
			if (i >= 0 && i < 1000) {
				for (int j = 0; j < 3; j++) {
					winnu[2 - j] = i % 10;
					i = i / 10;
				}
				return winnu;
			} else
				return winnu;
		} catch (Exception e) {
			return null;
		}
		//return winnu;
	}

	boolean getUserInput(String s) {
		s = s.trim();
		userNumber= new int [3];
		userInput = s;
		try {
			Integer i = Integer.valueOf(s);
			if (i >= 0 && i < 1000) {
				for (int j = 0; j < 3; j++) {
					userNumber[2 - j] = i % 10;
					i = i / 10;
				}
				return true;
			} else
				return false;
		} catch (Exception e) {
			return false;
		}
	}

	abstract int getwinlevel(int[] winnumber, String s);

	int getwinlevel() {
		return getwinlevel(this.winNumber, this.userInput);
	}
	int getwinlevel(String s) {
		return getwinlevel(this.winNumber,s);
	}

	public void setUserNumber(int[] userNumber) {
		this.userNumber = userNumber;
	}

}

class Group extends Lottery {
	Group(String s) {
		super(s);
		// TODO Auto-generated constructor stub
	}

	int[] arrange(int[] winnumber) {
		int[] newwinnumber = Arrays.copyOf(winnumber, winnumber.length);
		Arrays.sort(newwinnumber);
		return newwinnumber;
	}

	@Override
	int getwinlevel(int[] winnumber, String s) {
		// TODO Auto-generated method stub
		if (this.getUserInput(s)) {
			int[] newwinnumber = arrange(winnumber);
			int[] newuserinput = arrange(this.userNumber);
			int maxequal = 0;
			int currequal = 0;
			for (int i = 0; i < newwinnumber.length - 1; i++) {
				if (newwinnumber[i] == newwinnumber[i + 1]) {
					currequal++;
					if (currequal > maxequal) {
						maxequal = currequal;
					}
				} else
					currequal = 0;
			}
			boolean equals = true;

			for (int i = 0; i < newwinnumber.length - 1; i++) {
				if (newwinnumber[i] != newuserinput[i]) {
					equals = false;
					break;
				}
			}
			if (equals) {
				switch (maxequal) {
				case 0:
					return 173;
				case 1:
					return 346;
				default:
					return 0;
				}
			} else
				return 0;

		} else
			return 0;
	}

}

class Single extends Lottery {

	Single(String s) {
		super(s);
		// TODO Auto-generated constructor stub
	}

	@Override
	int getwinlevel(int[] winnumber, String s) {
		// TODO Auto-generated method stub
		if (this.getUserInput(s)) {
			boolean win = false;
			for (int i = 0; i < userNumber.length; i++) {
				if (userNumber[i] == winnumber[i]) {
					win = true;
				} else {
					win = false;
					break;
				}
			}
			if (win) {
				return 1040;

			} else
				return 0;
		} else {
			System.out.println("wrong input");
			return 0;
		}
		// return 0;
	}

}

public class Main {

	public static  Lottery getusertype(String s,String wins) {
		Lottery l=null;
		switch (s) {
		case "single":
			l=new Single(wins);
			break;
		case "group":
			l=new Group(wins);
			break;
		}
		return l;
		
	}
	public static void main(String[] arg) {
		Scanner s=new Scanner(System.in);
		String wins=s.next();
		int times=s.nextInt();
		int maxlevel=0;
		for(int i=0;i<times;i++) {
			 Lottery l;
			String type=s.next();
			l=getusertype(type,wins);
			String userinput=s.next();
			int curr=l.getwinlevel(userinput);
			if(maxlevel<curr) {
				maxlevel=curr;
			};
			
		}
		System.out.println(maxlevel);
				
	}
}
